﻿using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic;
using umbraco.cms.presentation.Trees;

namespace v76
{
    public partial class DictionaryPicker : System.Web.UI.UserControl, 
        umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
        private object _value;
        private TextBox control = new TextBox();
        public DictionaryPicker(){}
        protected string selectedValuePath;

        public object value
        {
            get
            {
                this._value = Request.Form[this.selectedEntry.UniqueID];                
                Setup();
                return this._value;
            }
            set
            {
                this._value = value;
                Setup();
                if (value != null)
                {
                    this.selectedEntry.Value = value.ToString();
                }
            }
        }
        
        private void Setup()
        {
            this.entries.Nodes.Clear();
            this.entries.ExpandImageUrl = "/umbraco_client/Tree/Themes/umbraco/fplus.gif";
            this.entries.CollapseImageUrl = "/umbraco_client/Tree/Themes/umbraco/fminus.gif";
            this.entries.PathSeparator ='/';
            this.entries.SelectedNodeStyle.BackColor = System.Drawing.Color.LightGray;
            var topItems = Dictionary.getTopMostItems;
            var rootNode = new TreeNode("Dictionary", "&nbsp;", "/umbraco/images/umbraco/folder.gif", "#", "_self");
            
            rootNode.SelectAction = TreeNodeSelectAction.None;
            this.entries.Nodes.Add(rootNode);
            LoadChildren(topItems, rootNode);
            this.entries.CollapseAll();
            Expand(this.entries.FindNode(selectedValuePath));
        }

        protected void Expand(TreeNode path)
        {
            var parent = path;
            while (parent.Depth>0)
            {               
                parent = parent.Parent;
                parent.Expand();
            }
            this.entries.Nodes[0].Expand();
            path.CollapseAll();
        }

        private void LoadChildren(Dictionary.DictionaryItem[] items, TreeNode parent)
        {
            foreach (var item in items)
            {
                var treeNode = new TreeNode(item.key, item.id.ToString(), "/umbraco/images/umbraco/settingDataType.gif", "javascript:dictionarypickerJS"+this.datatypecontainer.ClientID+".selectEntry('" + item.key.ToString() + "','" + item.key.ToString() + "')", "");
               
                parent.ChildNodes.Add(treeNode);

                if (this._value != null && this._value.ToString().Trim() == item.key.ToString().Trim())
                {
                    treeNode.Selected = true;
                    selectedValuePath = treeNode.ValuePath;
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.datatypecontainer.ClientID, "dictionarypickerJS" + this.datatypecontainer.ClientID + ".selectEntry('" + item.key.ToString() + "','" + item.key.ToString() + "');", true);
                }
                
                
                LoadChildren(item.Children, treeNode);
            }
        }
    }
}